package tw.twandroidproj.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import tw.twandroidproj.app.R;
import java.util.List;

import tw.twandroidproj.app.model.Job;
import tw.twandroidproj.app.ui.CircleImageView;
import tw.twandroidproj.app.ui.activity.details_job_Activity;

/**
 * Created by Raymon on 2017/2/21.
 */

public class mainRecyclerview_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //数据
    private List<Job> mjobs;
    // 设置底部布局
    private static final int TYPE_FOOTER = 0;
    // 设置默认布局
    private static final int TYPE_DEFAULT = 1;

    private Context mcontext;
    //判断是不是最后一个
    private boolean isShowFoot=true;

    public mainRecyclerview_Adapter(Context mcontext){

        this.mcontext=mcontext;

    }
    // 设置数据并刷新
    public void setData(List<Job> jobs)
    {
        Log.v("mainAdapter","sedata");
        this.mjobs = jobs;
        notifyDataSetChanged();
    }

    // 设置不同的item
    @Override
    public int getItemViewType(int position)
    {
        // 判断当前位置+1是不是等于数据总数（因为数组从0开始计数），是的就加载底部布局刷新，不是就加载默认布局
        if (position + 1 == getItemCount())
        {
            return TYPE_FOOTER;
        } else
        {
            return TYPE_DEFAULT;
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       if(viewType==TYPE_DEFAULT) {

           View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.job_detail_item, parent, false);
           final DefaultViewHolder holder = new DefaultViewHolder(view);
           holder.cilckView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   int position = holder.getAdapterPosition();
                   Job job = mjobs.get(position);
                   Intent intent = new Intent(v.getContext(), details_job_Activity.class);
                   intent.putExtra("JobId", job.getId());
                   v.getContext().startActivity(intent);
               }
           });
           return holder;
       }else {

           View view =LayoutInflater.from(parent.getContext()).inflate(R.layout.mainrecyclerview_foot,null);
           view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                   ViewGroup.LayoutParams.WRAP_CONTENT));
           return new FooterViewHolder(view);
       }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        // 用来在运行时指出对象是某一个对象
        if (holder instanceof DefaultViewHolder)
        {
            Job job = mjobs.get(position);

            if (job == null)
            {
                return;
            }

            ((DefaultViewHolder) holder).view_name.setText(job.getName());
            ((DefaultViewHolder) holder).view_salary.setText(job.getSalary());
        }
    }


    @Override
    public int getItemCount() {

        // 判断是不是显示底部，是就返回1，不是返回0
        int begin = isShowFoot? 1 : 0;
        // 没有数据的时候，直接返回begin
        if (mjobs == null)
        {
            return begin;
        }
        // 因为底部布局要占一个位置，所以总数目要+1
        return mjobs.size() + begin;
    }
    // 设置是否显示底部加载提示（将值传递给全局变量）
    public void isShowFooter(boolean showFooter)
    {
        this.isShowFoot = showFooter;
    }

    // 判断是否显示底部，数据来自全局变量
    public boolean isShowFooter()
    {
        return this.isShowFoot;
    }



    // 底部布局的ViewHolder
    public class FooterViewHolder extends RecyclerView.ViewHolder
    {
        public FooterViewHolder(View view)
        {
            super(view);
        }
    }

    // 默认布局的ViewHolder
    public class DefaultViewHolder extends RecyclerView.ViewHolder
    {
            View cilckView;
            CircleImageView imageview;
            TextView view_name;
            TextView view_time;
            TextView view_loc;
            TextView view_salary;
            TextView view_unit;
            public DefaultViewHolder(View itemView) {
            super(itemView);
            cilckView=itemView;
            imageview= (CircleImageView) itemView.findViewById(R.id.job_image_item);
            view_name= (TextView) itemView.findViewById(R.id.job_name_mainitem);
            view_time= (TextView) itemView.findViewById(R.id.job_time_main);
            view_loc= (TextView) itemView.findViewById(R.id.job_location_main);
            view_salary= (TextView) itemView.findViewById(R.id.job_salary_main);
            view_unit= (TextView) itemView.findViewById(R.id.job_salary_unit);
        }
    }


}
